package org.xxd.kafka.clients.common.requests;

import org.xxd.kafka.clients.common.network.NetworkSend;
import org.xxd.kafka.clients.common.protocol.types.Struct;

import java.nio.ByteBuffer;

/**
 * @author: XiaoDong.Xie
 * @create: 2020-09-28 10:21
 * @description:
 *  发送请求的对象
 */
public class RequestSend extends NetworkSend {
    private final RequestHeader header;
    private final Struct body;

    public RequestSend(String destination, RequestHeader header, Struct body) {
        super(destination, serialize(header, body));
        this.header = header;
        this.body = body;
    }

    public static ByteBuffer serialize(RequestHeader header, Struct body) {
        ByteBuffer buffer = ByteBuffer.allocate(header.sizeOf() + body.sizeOf());
        header.writeTo(buffer);
        body.writeTo(buffer);
        buffer.rewind();
        return buffer;
    }

    public RequestHeader header() {
        return this.header;
    }

    public Struct body() {
        return body;
    }

    @Override
    public String toString() {
        return "RequestSend(header=" + header.toString() + ", body=" + body.toString() + ")";
    }
}
